<?php

namespace Home\Controller;

class MapstoreController extends BaseStoreController {

    function _initialize() {
        parent::_initialize();
        $this->_user_id = session('visitor.user_id');
        $this->_store_mod = M('store');
    }

    /**
     * @api {get} /index.php?m=Home&c=mapstore&a=get_store_list 获取附近的店铺
     * @apiName 获取附近的店铺
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 获取附近的店铺
     * 
     * @apiParam {String} lat 纬度
     * @apiParam {String} lng 经度
     * @apiParam {String} scate_id 店铺分类ID
     * @apiParam {String} sort 排序名称
     * @apiParam {String} order 增序\降序
     * 
     * @apiSuccess {String} store_id 店铺ID
     * @apiSuccess {String} store_name 店铺名称
     * @apiSuccess {String} address 地址
     * @apiSuccess {String} store_logo 店铺LOGO
     * @apiSuccess {String} praise_rate 好评率
     * @apiSuccess {String} juli 距离
     * 
     */
    function get_store_list() {
        //经纬度
        $lat = I('request.lat', 0);
        $lng = I('request.lng', 0);
        $where = 'state=1';
        $scate_id = I('request.scate_id', 0);
        if ($scate_id) {
            $scate_ids = D('scategory')->get_descendant($scate_id);
            $where .= ' AND cate_id in(' . implode(',', $scate_ids) . ')';
        }

        //排序
        $sort = I('request.sort', '');
        $order = I('request.order', '');
        if (!in_array($sort, array('add_time', 'sgrade', 'credit_value', 'juli'))) {
            $sort = 'juli';
        }
        if (!in_array($order, array('desc', 'asc'))) {
            $order = 'desc';
        }
        //附近10KM店铺
//        $where .= ' AND (2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*(' . $lat . '-lat)/360),2)+COS(PI()*' . $lat . '/180)* COS(lat * PI()/180)*POW(SIN(PI()*(' . $lng . '-lng)/360),2)))) < 10';
        $store_list = $this->_store_mod
                ->field('store.store_id,store_name,address,store_logo,praise_rate,(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*(' . $lat . '-lat)/360),2)+COS(PI()*' . $lat . '/180)* COS(lat * PI()/180)*POW(SIN(PI()*(' . $lng . '-lng)/360),2)))) as juli ')
                ->alias('store')
                ->join('LEFT JOIN ' . C('DB_PREFIX') . 'category_store category_store ON store.store_id = category_store.store_id')
                ->where($where)
                ->page(I('request.page', 1, 'intval') . ',5')
                ->order($sort . ' ' . $order)
                ->select();
        $data = array(
            'store_list' => $store_list,
            'sort' => $sort,
            'order' => $order
        );
        render('10000', '', $data);
    }

    
    
    /**
     * @api {get} /index.php?m=Home&c=mapstore&a=store_goods 根据店铺ID获取商品信息
     * @apiName 根据店铺ID获取商品信息
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 根据店铺ID获取商品信息
     * 
     * @apiParam {String} store_id 店铺ID
     * 
     * @apiSuccess {String} store.store_id 店铺ID
     * @apiSuccess {String} store.store_name 店铺名称
     * @apiSuccess {String} store.service_arrive 抵达时间
     * @apiSuccess {String} store.service_consumption 平均消费
     * @apiSuccess {String} store.store_logo 店铺LOGO
     * @apiSuccess {String} store.region_name 地区名称
     * @apiSuccess {String} store.address 详细地址
     * @apiSuccess {String} store.evaluation_average 店铺评分平均值
     * @apiSuccess {String} store.evaluation_desc 店铺产品符合度
     * @apiSuccess {String} store.evaluation_service 店铺服务好评度
     * @apiSuccess {String} store.evaluation_speed 店铺发货速度
     * 
     * @apiSuccess {String} carts.amount 总价
     * @apiSuccess {String} carts.quantity 总数量
     * @apiSuccess {String} carts.goods.rec_id 购物车ID
     * @apiSuccess {String} carts.goods.spec_id 规格ID
     * @apiSuccess {String} carts.goods.price 价格
     * @apiSuccess {String} carts.goods.quantity 数量
     * 
     * @apiSuccess {String} categoods_list.id 店铺分类ID
     * @apiSuccess {String} categoods_list.value 店铺分类名称
     * @apiSuccess {String} categoods_list.count 商品总数量
     * @apiSuccess {String} categoods_list.goods_list.goods_id 商品ID
     * @apiSuccess {String} categoods_list.goods_list.goods_name 商品名称
     * @apiSuccess {String} categoods_list.goods_list.price 商品价格
     * @apiSuccess {String} categoods_list.goods_list.default_image 商品图片
     * @apiSuccess {String} categoods_list.goods_list.default_spec 默认规格
     * 
     * @apiSuccess {String} evaluation_list.buyer_id 买家ID
     * @apiSuccess {String} evaluation_list.buyer_name 买家会员名
     * @apiSuccess {String} evaluation_list.anonymous 是否匿名
     * @apiSuccess {String} evaluation_list.evaluation_time 评价时间
     * @apiSuccess {String} evaluation_list.goods_id 商品ID
     * @apiSuccess {String} evaluation_list.goods_name 商品名称
     * @apiSuccess {String} evaluation_list.specification 规格名称
     * @apiSuccess {String} evaluation_list.price 价格
     * @apiSuccess {String} evaluation_list.quantity 数量
     * @apiSuccess {String} evaluation_list.goods_image 商品图片
     * @apiSuccess {String} evaluation_list.evaluation 评价等级好中差
     * @apiSuccess {String} evaluation_list.comment 评论内容
     * 
     */
    function store_goods() {

        $store_id = I('request.store_id', 0, 'intval');
        if (empty($store_id)) {
            render('19999', '传入参数错误');
        }
        
        $store = $this->get_store_data($store_id);
        
        if ($this->_user_id) {
            $cart_mod = M('cart');
            $cart_items = $cart_mod
                    ->field('rec_id,spec_id,price,quantity')
                    ->where('user_id=' . $this->_user_id . ' AND store_id=' . $store_id)
                    ->select();
            
            if($cart_items){
                foreach ($cart_items as $key => $item) {
                    $item['subtotal'] = $item['price'] * $item['quantity'];
                    $carts['amount'] += $item['subtotal'];    //各店铺的总金额
                    $carts['quantity'] += $item['quantity'];   //各店铺的总数量
                    $carts['goods'][$item['spec_id']] = $item;
                }
            }
        }
        
        $gcategories = D('gcategory')->get_list(-1, true,$store_id);
        import("Org.Com.Tree");
        $tree = new \Org\Com\Tree;
        $tree->setTree($gcategories, 'cate_id', 'parent_id', 'cate_name');
        $gcategories = $tree->getArrayList(0);
        
        $categoods_list = array();
        $goods_mod = M('goods');
        foreach ($gcategories as $gcategorie_key => $value) {
            //获取分类以及子分类
            $sgcate_ids = D('gcategory')->get_descendant($value['id']);
            $where = 'closed = 0 AND if_show = 1 AND category_goods.cate_id in(' . implode(',', $sgcate_ids) . ')';
            $goods_list = $goods_mod
                    ->field('goods.goods_id,goods_name,price,default_image,default_spec')
                    ->alias('goods')
                    ->where($where)
                    ->join('LEFT JOIN ' . C('DB_PREFIX') . 'category_goods category_goods ON goods.goods_id = category_goods.goods_id')
                    ->order('add_time desc')
                    ->select();
            foreach ($goods_list as $goods_key => $goods) {
                //此处需要通过 规格ID 判断
                if($carts&&array_key_exists($goods['default_spec'], $carts['goods'])){
                    $goods_list[$goods_key]['quantity'] = $carts['goods'][$goods['default_spec']]['quantity'];
                }
            }
            $categoods_list[$gcategorie_key] = $value;
            $categoods_list[$gcategorie_key]['count'] = count($goods_list);
            $categoods_list[$gcategorie_key]['goods_list'] = $goods_list;
        }
        
        //获取所有评价
        $order_goods_mod = M('order_goods');
        $evaluation_list = $order_goods_mod
                ->field('buyer_id, buyer_name, anonymous, evaluation_time, goods_id, goods_name, specification, price, quantity, goods_image, evaluation, comment')
                ->alias('og')
                ->join('LEFT JOIN ' . C('DB_PREFIX') . 'order o ON o.order_id = og.order_id')
                ->where('seller_id = '.$store_id.' AND evaluation_status = 1 AND is_valid = 1 ')
                ->page(I('request.page', 1, 'intval') . ',10')
                ->select();
        if($evaluation_list){
            foreach ($evaluation_list as $eval_key => $evaluation) {
                $evaluation_list[$eval_key]['evaluation_time'] = date('Y-m-d H:i:s',$evaluation['evaluation_time']);
            }
        }
        
        $data = array(
            'store'=>$store,
            'carts'=>$carts,
            'categoods_list'=>$categoods_list,
            'evaluation_list'=>$evaluation_list,
        );
        render('10000', '', $data);
    }
    
    /**
     * @api {get} /index.php?m=Home&c=mapstore&a=scategory 获取顶级店铺分类
     * @apiName 获取顶级店铺分类
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 获取顶级店铺分类
     * 
     * @apiSuccess {String} cate_id 店铺分类ID
     * @apiSuccess {String} cate_name 店铺分类名称
     * @apiSuccess {String} sort_order 店铺分类排序
     * 
     */
    function scategory()
    {
        $scategory_list = M('scategory')->where('parent_id=0')->order('sort_order asc')->select();
        render('10000', '', array('scategory_list'=>$scategory_list));
    }
    
    
    
    

}
